package com.example.emos.workflow.bpmn;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONObject;
import com.example.emos.workflow.config.quartz.MeetingRoomJob;
import com.example.emos.workflow.config.quartz.MeetingStatusJob;
import com.example.emos.workflow.config.quartz.MeetingWorkflowJob;
import com.example.emos.workflow.config.quartz.QuartzUtil;
import com.example.emos.workflow.service.MeetingService;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class NotifyMeetingService implements JavaDelegate {
    @Autowired
    private QuartzUtil quartzUtil;

    @Autowired
    private MeetingService meetingService;

    @Override
    public void execute(DelegateExecution delegateExecution) {
        System.out.println("会议TaskService");
        Map map = delegateExecution.getVariables();
        String uuid = MapUtil.getStr(map, "uuid");
//        String url = MapUtil.getStr(map, "url");
        String result = MapUtil.getStr(map, "result");
//        String creatorName = MapUtil.getStr(map, "creatorName");
//        Integer creatorId = MapUtil.getInt(map, "creatorId");
//        String identity = MapUtil.getStr(map, "identity");
//        Integer managerId = MapUtil.getInt(map, "managerId");
//        Integer gmId = MapUtil.getInt(map, "gmId");
//        String sameDept = MapUtil.getStr(map, "sameDept");

        HashMap data = meetingService.searchMeetingByUUID(uuid);
//        String title = MapUtil.getStr(data, "title");
        String date = MapUtil.getStr(data, "date");
        String start = MapUtil.getStr(data, "start");
        String end = MapUtil.getStr(data, "end");

        map.put("end", end);
        String meetingType = delegateExecution.getVariable("meetingType", String.class);

        if (result.equals("同意")) {
            meetingService.updateMeetingStatus(new HashMap() {{
                put("uuid", uuid);
                put("status", 3);
            }});

            if (meetingType.equals("线上会议")) {
                JobDetail jobDetail = JobBuilder.newJob(MeetingRoomJob.class).build();
                Map mapBegin = jobDetail.getJobDataMap();
                mapBegin.putAll(map);
                Date expire = DateUtil.parse(date + " " + end, "yyyy-MM-dd HH:mm");
                mapBegin.put("expire", expire);
                Date executeDate = DateUtil.parse(date + " " + start, "yyyy-MM-dd HH:mm").offset(DateField.MINUTE, -15);

//                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
//                String dateStr = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳s
//                Date executeDate = DateUtil.parse(dateStr, "yyyy-MM-dd HH:mm");

                quartzUtil.addJob(jobDetail, uuid, "创建会议室ID任务组", executeDate);


//        status 状态（1.申请中，2.审批未通过，3.审批通过，4.会议进行中，5.会议结束）
                //添加会议进行中 定时任务
                jobDetail = JobBuilder.newJob(MeetingStatusJob.class).build();
                Map mapProgress = jobDetail.getJobDataMap();
                mapProgress.putAll(map);
                mapProgress.put("status", 4);
                executeDate = DateUtil.parse(date + " " + start, "yyyy-MM-dd HH:mm");

//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
//        String dateStr = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳s
//        Date executeDate1 = DateUtil.parse(dateStr, "yyyy-MM-dd HH:mm");

                quartzUtil.addJob(jobDetail, uuid, "会议开始任务组", executeDate);

                //添加会议结束定时任务
                jobDetail = JobBuilder.newJob(MeetingStatusJob.class).build();
                Map mapFinish = jobDetail.getJobDataMap();
                mapFinish.putAll(map);
                mapFinish.put("status", 5);
                mapFinish.put("flag", "end");

//              用于测试
//                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
//                String dateStr = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳s
//                executeDate = DateUtil.parse(dateStr, "yyyy-MM-dd HH:mm");

                executeDate = DateUtil.parse(date + " " + end, "yyyy-MM-dd HH:mm");
                quartzUtil.addJob(jobDetail, uuid, "会议结束任务组", executeDate);

                JSONObject json = new JSONObject();
                String processId = delegateExecution.getProcessInstanceId();
                json.set("processId", processId);

            }

        } else {
            meetingService.updateMeetingStatus(new HashMap() {{
                put("uuid", uuid);
                put("status", 2);
            }});
        }
        //quartzUtil.deleteJob(uuid, "会议工作流组");

        //审批完成 开启工作流任务组发短信通知 同意就发给所有要参会的人 不同意只发给 会议创建者。
        JobDetail jobDetail = JobBuilder.newJob(MeetingWorkflowJob.class).build();
        String instanceId = delegateExecution.getProcessInstanceId();
        Map dataMap = jobDetail.getJobDataMap();
        dataMap.putAll(map);
        dataMap.put("instanceId", instanceId);
        //Date executeDate1 = DateUtil.parse(date + " " + start, "yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String dateStr = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳s
        Date executeDate = DateUtil.parse(dateStr).offset(DateField.MINUTE, 1);//定时器在一分钟后触发
        //Date executeDate = DateUtil.parse(dateStr);//定时器立即触发
        quartzUtil.addJob(jobDetail, uuid, "会议工作流组", executeDate);
    }
}